<?php

// 优惠券

namespace app\shop\controller;

use think\Db;
use think\Request;
use think\Session;
use think\Controller;
use app\shop\model\Coupon as CouponModel;
use app\shop\model\GoodsType as GoodsTypeModel;
use app\shop\model\MemberCoupon as MemberCouponModel;
use app\shop\model\MemberRedPacket as MemberRedPacketModel;

class Coupon extends Controller
{
    /**
     * 优惠券主页
     * @param CouponModel $coupon
     * @return mixed
     */
    public function index(CouponModel $coupon)
    {
        $ini['end_time'] = ['gt',time()];
        $data = $coupon->where(['member_id' => 0, 'status' => '1'])->where($ini)
            ->field('id,title,actual_price,virtual_price,number,content,start_time,end_time,sort as receive_status,parenTid')
            ->with('GoodsType')
            ->with(['MemberCoupon' => function ($e) {
                $e->where(['member_id' => session('member_id')])->field('id,coupon_id,status,member_id');

            }])->order(['id' => 'desc'])->paginate(6);




        return $this->fetch('', [
            'mate_head' => '领券中心',
            'data'      => $data,
        ]);
    }


    /**
     * 领取优惠券
     * @param Request $request
     * @param CouponModel $coupon
     * @param MemberCouponModel $memberCoupon
     * @return array
     */
    public function receive_coupons(Request $request, CouponModel $coupon, MemberCouponModel $memberCoupon)
    {
        // 获取用户ID 优惠券ID
        $memberId = Session::get('member_id');

        // 抛出异常
        if (empty($memberId)) return ['code' => 402, 'message' => '你未登录！请先登录'];


        $coupon_id = $request->param('coupon_id');

        // 获取优惠券信息
        $coupons = (new CouponModel())->where(['id' => $coupon_id])->find();

        // 查询是否领取过
        $id = $memberCoupon->where(['coupon_id' => $coupon_id, 'member_id' => $memberId])->value('id');

        // 判断失败状态
        if ($memberId == $coupons['member_id']) return ['code' => 401, 'message' => '不能领取自己的优惠券'];
        if ($coupons['number'] <= '0') return ['code' => 401, 'message' => '优惠券已被领完,下次要早点哦'];
        if (!empty($id)) return ['code' => 401, 'message' => '您已经领取过该优惠券'];

        Db::startTrans();

        try {
            // 获取信息
            $data = [
                'member_id'     => $memberId,
                'shop_id'       => $coupons['member_id'],
                'coupon_id'     => $coupon_id,
                'title'         => $coupons['title'],
                'actual_price'  => $coupons['actual_price'],
                'virtual_price' => $coupons['virtual_price'],
                'content'       => $coupons['content'],
                'start_time'    => $coupons['start_time'],
                'end_time'      => $coupons['end_time'],
                'status'        => '2',
                'create_time'   => time()
            ];

            $memberCoupon->allowField(true)->save($data);
            Db::commit();
            return ['code' => 200];

        } catch (\Exception $e) {

            Db::rollback();

            return ['code' => 401, 'message' => '服务器繁忙稍后再试'];
        }


    }


    /**
     * 使用优惠卷
     */
    public function user_coupon()
    {
        // 获取参数
        $data = Request::instance()->param();
        // 查找此优惠卷
        $coupon = (new MemberCouponModel())->where(['id'=>$data['coupon_id']])->find()->toArray();

        // 判断当前价格是否够减免
        if ($coupon['virtual_price']>$data['price']) {
            return ['code'=>402,'message'=>'满减金额不够！'];
        }else{
            return ['code'=>200,'message'=>$coupon['virtual_price']];
        }

    }


    /**
     * 使用红包
     */
    public function user_red_packet()
    {
        // 获取参数
        $data = Request::instance()->param();

        // 查找红包
        $redPacket = (new MemberRedPacketModel())->where(['id'=>$data['red_id']])->find()->toArray();

        // 判断当前价格是否够减免
        if ($redPacket['virtual_price']>$data['price']) {
            return ['code'=>402,'message'=>'满减金额不够！'];
        }else{
            return ['code'=>200,'message'=>$redPacket['virtual_price']];
        }

    }
}